//获取应用实例
const app = getApp()
Page({
  data: {
    themeVal: app.globalData.theme,
    themeBgObj: {
      themeA: '#ffeb0c',
      themeB: '#ff4500'
    },
    orderIndex: 1,
    data: [],
    allList: [

    ],
    cancelList: [],
    doneList: [

    ],
    isPayList: [

    ],
    noMore: false,
    Permission: app.globalData.token ? true : false,
    enterType: 'init'
  },
  onLoad: function (options) {
    if (JSON.stringify(options) != "{}") {
      this.setData({
        enterType: options.type
      })
    }

  },

  orderTab(e) {
    let orderIndex = e.currentTarget.dataset.tab;
    this.setData({
      orderIndex: orderIndex
    })

  },
  toDetail(e) {
    let item = e.currentTarget.dataset.item;
    app.Store.set("enterType", this.data.enterType)
    app.WxService.navigateTo(`/pages/order/detail/index`, { order_sn: item.order_sn, enterType: this.data.enterType })
  },
  onShow: function () {
    this.setData({
      Permission: app.globalData.token ? true : false
    })
    if (app.Store.get("enterType")) {
      this.setData({
        enterType: app.Store.get("enterType")
      })
    }
    if (this.data.enterType === 'init') {
      wx.setNavigationBarTitle({
        title: '我的订单'
      })
    } else if (this.data.enterType === 'user') {
      wx.setNavigationBarTitle({
        title: '店铺订单管理'
      })
    } else {
      wx.setNavigationBarTitle({
        title: '进货订单管理'
      })
    }
    this.getOrderList(this.data.enterType)
  },
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this.getOrderList(this.data.enterType)
    setTimeout(() => {
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
    }, 10);
  },
  onReachBottom: function () {
    wx.startPullDownRefresh()
  },
  getOrderList(type) {
    let url = type === 'user' ? app.Api.OrderByShopId : type === 'supply' ? app.Api.OrderShopList : app.Api.OrderList
    app.Http.http({
      url: url,
      method: 'POST',
      params: {}
    }).then(data => {
      let result = data
      if (result.length > 0) {
        let doneList = result.filter((item) => {
          return item.order_status == 0
        })
        let cancelList = result.filter((item) => {
          return item.order_status == 1
        })
        let isPayList = result.filter((item) => {
          return item.order_status == 2
        })
        this.setData({
          allList: result,
          cancelList: cancelList,
          isPayList: isPayList,
          doneList: doneList
        })
      } else {
        this.setData({
          allList: [],
          cancelList: [],
          isPayList: [],
          doneList: []
        })
      }

    }).catch(err => {
      console.log(err)
    })
  },
  cancelOrder(e) {
    let _this = this;
    let item = e.currentTarget.dataset.item
    let url = this.data.enterType === 'user' ? app.Api.OrderShopCancel : app.Api.OrderCancel
    wx.showModal({
      title: '温馨提示',
      content: '确定要取消该订单吗？',
      success(res) {
        if (res.confirm) {
          app.Http.http({
            url: url,
            method: 'POST',
            params: {
              order_sn: item.order_sn
            }
          }).then(data => {
            _this.getOrderList(_this.data.enterType)
          }).catch(err => {
            console.log(err)
          })
        }
      }
    })

  },
  payOrder(e) {
    let item = e.currentTarget.dataset.item
    app.payOrder({ order_sn: item.order_sn }, (data) => {
      this.getOrderList(this.data.enterType)
    })

  },
  payEmbody(e){
    let item = e.currentTarget.dataset.item
    
    app.Http.http({
      url: app.Api.ApplyTranfer,
      method: 'POST',
      params: { order_sn: item.order_sn}
    }).then(data => {
     console.log(data)

    }).catch(err => {
      console.log(err)
    })
  },
  changeData(data) {

    if (this.data.enterType === 'init') {
      wx.setNavigationBarTitle({
        title: '我的订单'
      })
    } else if (this.data.enterType === 'user') {
      wx.setNavigationBarTitle({
        title: '店铺订单管理'
      })
    } else {
      wx.setNavigationBarTitle({
        title: '进货订单管理'
      })
    }
    this.getOrderList(this.data.enterType)
  }
})